import { Button, Input, Select } from 'antd';
import Editor from 'components/Editor';
import React, { useState } from 'react';
const md = require('./index.md').default
const POSITION: any = Object.freeze({
    left: Symbol(),
    right: Symbol(),
    both: Symbol(),
    center: Symbol(),
    all: Symbol()
})
export default () => {
    const trim = (str: string, position = POSITION.both) => {
        if (!!POSITION[position]) throw new Error('unexpected position value')
        switch (position) {
            case (POSITION.left):
                str = str.replace(/^\s+/, '')
                break
            case (POSITION.right):
                str = str.replace(/\s+$/, '')
                break
            case (POSITION.both):
                str = str.replace(/^\s+/, '').replace(/\s+$/, '')
                break
            case (POSITION.center):
                while(str.match(/\w\s+\w/)) {
                    str = str.replace(/(\w)\s+(\w)/, '$1$2')
                }
                break
            case (POSITION.all):
                str = str.replace(/\s/g, '')
                break
            default:
                
        }
        return str
    }
    const onTrimClick = () => {
        setResult(trim(str))
    }
    const [str, setStr] = useState('')
    const [result, setResult] = useState('')
    return (
        <div>
            <div>写一个方法去掉字符串空格</div>
            <Input value={str} onChange={(e) => setStr(e.target.value)} style={{ width: 200 }} />
            <Button onClick={onTrimClick}>trim</Button>
            {result}
            <Editor value={md}/>
        </div>
    )
}